var isTabOpen = false;

function show() {
	document.getElementById("addBox").style.display = "";
}

function $(id) {
	return document.getElementById(id);
}

function createSidebar(data) {
	if (data.drAvailableSites == null || data.drAvailableSites.indexOf(window.location.host) < 0) {
		return;
	}

	var width = window.getComputedStyle(document.body, null).getPropertyValue("width");
	var sidebar = document.createElement('div');
	sidebar.setAttribute("id", "drSidebar");
	sidebar.setAttribute("class", "drSidebarScrollbar");

	var tabDiv = document.createElement('div');
	tabDiv.setAttribute("id", "drSidebarTab");
	tabDiv.style.backgroundImage = "url("+chrome.extension.getURL('images/durian-tab.png')+")";

	var boxDiv = document.createElement("div");
	boxDiv.setAttribute("id", "drBox");
	boxDiv.style.display = "none";

    var closeBtn = document.createElement("DIV");
    closeBtn.setAttribute("id", "close");
    closeBtn.innerText = "关闭";
    closeBtn.style.float = "right";
    closeBtn.onclick = function() { closeBox(); };

    var formDiv = document.createElement("DIV");
    formDiv.setAttribute("id", "drForm");
    formDiv.innerHTML = "当价格低于<input id='priceline' type='text' />提醒我<br />";

	var saveBtn = document.createElement('INPUT');
	saveBtn.setAttribute("id", "save");
	saveBtn.setAttribute("type", "button");
	saveBtn.setAttribute("value", "保存提醒");
	saveBtn.onclick = function() { saveAlert(); closeBox(); };

    boxDiv.appendChild(closeBtn);
    boxDiv.appendChild(formDiv);
	boxDiv.appendChild(saveBtn);
	sidebar.appendChild(tabDiv);
	sidebar.appendChild(boxDiv);
	document.body.insertBefore(sidebar, document.body.firstChild);

	$("drSidebarTab").onclick = function(){
		if (isTabOpen) {
			closeBox();
		} else {
			openBox();
		}
	};

    chrome.extension.sendRequest({}, function(response) { loadPrice(response); });
}

function loadPrice (data) {
    $('priceline').value = data;
}

function openBox() {
	var box = $('drBox');
	box.style.display = "";
	box.setAttribute("class", "drBoxOpen");
    $('priceline').focus();
	isTabOpen = true;
}

function closeBox() {
	$('drBox').style.display = "none";
	isTabOpen = false;
}

function saveAlert() {
	data = { 'name':document.title, 'url' : window.location.href, 'priceline':$('priceline').value, 'site': window.location.host};
	chrome.extension.sendRequest( { 'action' : 'addItem', 'data' : data }, function() {})
}

function something() {
	var el = document.createElement("DIV");
	el.innerHTML = "<font color='red'>" + window.location + "</font><input id='btnExtend' type='button' value='增加提醒' onclick='show()' /><div id='addBox' style='display:none'><label>输入提醒价格:</label><input id='alertPrice' type='text' value='' /><br /><input id='btnSave' type='button' value='保存' /></div>";
	el.setAttribute("style", "position: fixed;top:0;right:0;border:solid 1px;");
	document.body.appendChild(el);
}

function convertTaobaoItem(data) {
}

function juandou() {
    var _jdt = new Date().toDateString().replace(/\s/g, '');
    var jdscript = document.createElement("script");
    jdscript.src = "http://js.juandou.com/a.js?v=" + _jdt + ".js";
    document.getElementByTagName("body").appendChild(jdscript);
/*
    try{
      var _jd = JuanDou.__init__();
      _jd._setCID(10043230);
      _jd._run();
    }catch(e){}
*/
}

chrome.extension.sendRequest(
	{'action' : 'load'},
	function(response) {
		createSidebar(response);
	}
);
chrome.extension.sendRequest({'action':'tk', 'currentUrl': window.location.href}, function(response) { convertTaobaoItem(response); });
chrome.extension.sendRequest({'action':'juandou'}, function(response) { juandou(); });
